package com.ykht.proj.activity.mq.producer;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ykht.proj.activity.mq.MqConfig;
import com.ykht.proj.activity.mq.init.Producer;
import com.ykht.proj.comsrv.constant.ComConstant;
import com.ykht.proj.comsrv.vo.SubscribeMsg;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.json.JSONObject;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Scanner;


/**
 * 原生的生产者，不想改了，coupon服务用的时基于RocketMQTemplate的
 */
@Data
@Slf4j
@Component
public class MsgProducer {

	@Resource
	private  Producer defaultMQProducer;

	public void sendMsg(SubscribeMsg msg) throws Exception {
		log.info("发送的原始数据{}",msg);
		DefaultMQProducer producer = defaultMQProducer.getDefProducer();
		Message message = new Message(ComConstant.SUBSCRIBE_MSG_TOPIC, StrUtil.utf8Bytes(JSON.toJSONString(msg)));
		// 设置延时等级3,这个消息将在10s之后发送(现在只支持固定的几个时间,详看delayTimeLevel)1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
		// 发送消息
		message.setTags(msg.getType());
		producer.send(message);
		log.info("发送消息：{}",JSONUtil.toJsonStr(message));

	}
}